<?php

namespace App\Http\Controllers\Api\V1\Requests;

use App\Models\User;
use App\Supports\Sms\Sms;
use Closure;

class AuthAlipayBindRequest extends FormRequest
{
    public function rules()
    {
	    

	    return [
		    'sms_key' => 'required|string',
			'sms_code' => ['required', 'string', $this->smsCode()],
	    ];
    }

   public function smsCode(): Closure
	{
		return function ($attribute, $value, $fail) {
			$sms = app(Sms::class);
			$verification = $sms->isSmsDataRight(request('sms_key'),request()->user()->phone, $value);

			$key = "auth_alipay_".request()->user()->phone;

			if($sms->isFailCountOver($key)){

				return $fail('验证码申请过于频繁，请稍后再试');
			}

			if (! $verification) {
				$sms->incrFailCount($key);	
				return $fail('短信验证码无效');

			}
			return true;
		};
	}

}
